package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishSeriveImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 新增菜品及其对应的口味
     * @param dishDTO
     */
    @Transactional //开启事务(保证原子性)
    public void insertWithFlavor(DishDTO dishDTO) {

        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setStatus(StatusConstant.DISABLE);
        //插入一条菜品数据
        dishMapper.insert(dish);

        //获取菜品ID
        Long dishId=dish.getId();


        List<DishFlavor>flavors=dishDTO.getFlavors();
        //插入多条口味数据
        if (flavors!=null&&flavors.size()>0){
            flavors.forEach(dishFlavor ->{
                dishFlavor.setDishId(dishId);
            });
            //向口味表插入数据
            dishFlavorMapper.insert(flavors);
        }

    }

    /**
     * 分页查询菜品
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult query(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page=dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 删除菜品
     * @param ids
     */
    @Transactional
    public void delete(List<Long> ids) {

        for (Long id:ids){
            //在售状态下，不可删除菜品
            //根据ID查询菜品，判断是否在售
            Dish dish=dishMapper.queryById(id);

            //被套餐关联的菜品不能删除
            //根据菜品ID查询套餐ID的个数，判断是否被套餐关联
            List<Long> setmealIds =setmealDishMapper.getSetmealIdByDishId(id);

            if(dish.getStatus()==StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            } else if (setmealIds.size()>0) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
//            else {
//                //删除菜品数据
//                dishMapper.delete(id);
//                //删除菜品关联的口味数据
//                dishFlavorMapper.delete(id);
//            }
        }
                //根据菜品ID集合删除菜品数据
              dishMapper.deleteByIds(ids);
//                //根据菜品ID集合删除菜品关联的口味数据
              dishFlavorMapper.deleteByDishIds(ids);
    }

    /**
     *更新菜品数据
     * @param dishDTO
     */
    @Transactional
    public void update(DishDTO dishDTO) {
        //更新菜品数据
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //更新菜品关联的口味数据-------删除原有的口味，再添加
        dishFlavorMapper.delete(dish.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            flavors.forEach(dishFlavor ->{
                dishFlavor.setDishId(dish.getId());
            });
            dishFlavorMapper.insert(flavors);
        }
    }

    /**
     * 根据ID查询菜品
     * @param id
     * @return
     */
    public DishVO queryById(Long id) {
        //根据ID查询查询菜品数据
        Dish dish=dishMapper.queryById(id);

        //根据ID查询菜品关联的口味数据
        List<DishFlavor> dishFlavor=dishFlavorMapper.selectByDishId(id);
        DishVO dishVO = new DishVO();
        if (dishFlavor != null) {
            dishVO.setFlavors(dishFlavor);
        }
        if(dish!=null) {
            BeanUtils.copyProperties(dish, dishVO);
        }
        return dishVO;
    }

    /**
     * 启用禁用菜品
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        //菜品停售，则包含菜品的套餐同时停售
        Dish dish=Dish.builder().status(status).id(id).build();
        dishMapper.update(dish);
        //通过菜品ID查询套餐ID
        List<Long> setmealIds = setmealDishMapper.getSetmealIdByDishId(id);
        setmealIds.forEach(setmealId ->{
            Setmeal setmeal=Setmeal.builder().status(status).id(setmealId).build();
            setmealMapper.update(setmeal);
        });

    }

    /**
     * 根据分类ID查询菜品
     * @param categoryId
     * @return
     */
    public List<Dish> queryByCategoryId(Long categoryId) {
        Dish dish=Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        List<Dish> dishes = dishMapper.query(dish);
        return dishes;
    }

    /**
     * 根据套餐ID查询菜品
     * @param id
     * @return
     */
    public List<DishItemVO> queryBySetmealId(Long id) {
        List<DishItemVO>dishItemVOS=dishMapper.queryBySetmealId(id);
        return dishItemVOS;
    }

    /**
     * 根据分类ID查询菜品及其口味
     * @param categoryId
     * @return
     */
    public List<DishVO> getByCategoryId(Long categoryId) {
        //根据分类Id查询菜品
        List<DishVO> dishVOS=new ArrayList<>();
        dishVOS = dishMapper.queryByCategoryId(categoryId);

        //根据菜品ID查询口味
        dishVOS.forEach(ds->{
            Long id = ds.getId();
            List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(id);
            ds.setFlavors(flavors);
        });
        return dishVOS;
    }
}
